package com.media.module.fragment;

import java.util.List;

import com.media.module.R;
import com.media.module.utils.CameraUtils;
import com.media.module.utils.DimenValues;
import com.media.module.utils.MediaConst;
import com.media.module.utils.UIGlobalDef;
import com.media.module.widget.FractionTranslateLayout;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 设置页面
 * @author wangqingcai
 *
 */
public class SettingFragment extends BaseFragment implements OnClickListener,OnTouchListener {
	
	private  Context mContext;
	private ViewGroup mvgContent;
	private float preXvaule;
	private RadioButton mRadioBtn;
	private int mintPosition = 1;
	private LinearLayout mlayoutCon;
	private RelativeLayout layout;
	private List<Camera.Size> msPictureSizes;
	

	public SettingFragment(Context context) {
		mContext = context;
		msPictureSizes  = CameraUtils.getPictureSizes(mContext);
		 String datas = DimenValues.getInstance(mContext).getSharePreferencesValue(UIGlobalDef.STR_RSP_TAKE_PHOTO_RESOLUTION_MAP);
	        if(datas.isEmpty())return;
	        String[] resolutions = datas.split("-");
	        for(int i=0;i<msPictureSizes.size() ;i++){
	            if(msPictureSizes.get(i).width==Integer.valueOf(resolutions[0])&&msPictureSizes.get(i).height==Integer.valueOf(resolutions[1])){
	                mintPosition= i+1;
	                break;
	            }
	        }
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		DisplayMetrics displayMetrics = new DisplayMetrics();
	    activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
		mvgContent = new FractionTranslateLayout(mContext);
		mvgContent.setBackgroundColor(Color.BLACK);
		LinearLayout.LayoutParams conParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		mlayoutCon = new LinearLayout(mContext);
		mlayoutCon.setOrientation(LinearLayout.VERTICAL);
		mlayoutCon.setGravity(Gravity.CENTER_HORIZONTAL);
		TextView tvTittle = new TextView(mContext);
		tvTittle.setTextSize(DimenValues.spToPx(mContext, 7));
		tvTittle.setTextColor(Color.WHITE);
		tvTittle.setText("分辨率");
		RelativeLayout.LayoutParams tittleParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		tittleParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
		tittleParams.setMargins(0, DimenValues.dipToPx(mContext, 60), 0, DimenValues.dipToPx(mContext, 120));
		mlayoutCon.addView(tvTittle, tittleParams);
		for(Camera.Size size:msPictureSizes){
			layout = new RelativeLayout(mContext);
			RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			
			
			RelativeLayout.LayoutParams reParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			reParams.addRule(RelativeLayout.START_OF, RelativeLayout.TRUE);
			reParams.setMargins(DimenValues.dipToPx(mContext, 20), DimenValues.dipToPx(mContext, 12), 0,DimenValues.dipToPx(mContext, 12));
			reParams.addRule(RelativeLayout.CENTER_VERTICAL,  RelativeLayout.TRUE);
			TextView textView = new TextView(mContext);
			textView.setTextSize(DimenValues.spToPx(mContext, 5));
			textView.setTextColor(Color.WHITE);
			textView.setText(Math.ceil((double)size.height*(double)size.width/1000000) +"M "+size.width+ "X" +size.height+" （"+size.width/ CameraUtils.minScale(size.height,size.width)+":"+size.height/CameraUtils.minScale(size.height,size.width)+")");
			
			mRadioBtn = new  RadioButton(mContext);
			mRadioBtn.setClickable(false);
			RelativeLayout.LayoutParams radioBtnParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			radioBtnParams.addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
			radioBtnParams.setMarginEnd(DimenValues.dipToPx(mContext, 20));
			radioBtnParams.addRule(RelativeLayout.CENTER_VERTICAL,  RelativeLayout.TRUE);
			
			layout.addView(textView, reParams);
			layout.addView(mRadioBtn, radioBtnParams);
			layout.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					((RadioButton)((RelativeLayout)v).getChildAt(1)).setChecked(true);
					RelativeLayout view = (RelativeLayout) mlayoutCon.getChildAt(mintPosition);
					((RadioButton)view.getChildAt(1)).setChecked(false);
					 mintPosition = mlayoutCon.indexOfChild(v);
					 Bundle bundle = ((Activity)mContext).getIntent().getBundleExtra(MediaConst.BUNDLE_DATA);
		             bundle.putInt(MediaConst.OUTPUT_PHOTO_HEIGHT_PX, msPictureSizes.get(mintPosition-1).height);
		             bundle.putInt(MediaConst.OUTPUT_PHOTO_WIDTH_PX, msPictureSizes.get(mintPosition-1).width);
		             TakePhotoFragment fragment = new TakePhotoFragment(mContext, bundle);
		             DimenValues.getInstance(mContext).setSharedPreferences(UIGlobalDef.STR_RSP_TAKE_PHOTO_RESOLUTION_MAP,msPictureSizes.get(mintPosition-1).width+"-"+msPictureSizes.get(mintPosition-1).height);
		             ((Activity)mContext).getFragmentManager().beginTransaction()
		             .setCustomAnimations(R.animator.fragment_slide_left_in, R.animator.fragment_slide_right_out)
		             .replace(UIGlobalDef.VIEW_ID.FRAME_LAYOUT, fragment).addToBackStack(null).commit();
				}
			});
			mlayoutCon.addView(layout,layoutParams);
			if(mlayoutCon.indexOfChild(layout)== mintPosition){
				mRadioBtn.setChecked(true);
			}
		}
		mvgContent.addView(mlayoutCon, conParams);
		mvgContent.setOnTouchListener(this);
		return mvgContent;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		
		
	}

	@SuppressWarnings("deprecation")
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		 switch (event.getAction()) {
         case MotionEvent.ACTION_DOWN:
             preXvaule = event.getX();
             break;
         case MotionEvent.ACTION_MOVE:
             float motionValue = event.getX() - preXvaule;
             if (motionValue < 0||Math.abs(motionValue)<DimenValues.dipToPx(mContext, 40)) {
                 return true;
             }
             preXvaule = event.getX();
             Bundle bundle = ((Activity)mContext).getIntent().getBundleExtra(MediaConst.BUNDLE_DATA);
             TakePhotoFragment fragment = new TakePhotoFragment(mContext, bundle);
             DimenValues.getInstance(mContext).setSharedPreferences(UIGlobalDef.STR_RSP_TAKE_PHOTO_RESOLUTION_MAP,msPictureSizes.get(mintPosition-1).width+"-"+msPictureSizes.get(mintPosition-1).height);
             ((Activity)mContext).getFragmentManager().beginTransaction()
             .setCustomAnimations(R.animator.fragment_slide_left_in, R.animator.fragment_slide_right_out)
             .replace(UIGlobalDef.VIEW_ID.FRAME_LAYOUT, fragment).addToBackStack(null).commit();
             break;
         default:
             break;
		 } 
         return true;
	}

}
